//
//  ActivityLinkView.m
//  LovingPartyBuilding
//
//  Created by 刘应平 on 17/5/1.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "BranchPositionView.h"
#import "BranchPostionTableViewCell.h"
#import "BranchPartyOrgizationModel.h"

@interface BranchPositionView ()<UITableViewDelegate,UITableViewDataSource>
/**  tableVie  */
@property (strong, nonatomic) UITableView  *tableView;

/**  数据  */
@property (strong, nonatomic) NSMutableArray  *dataSouce;

/**  页数  */
@property (assign, nonatomic) NSInteger  page;
/** 滑动 */
@property (nonatomic, strong) ZJRefurbish *refurbish;



@end

@implementation BranchPositionView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        __weak typeof(self) weakSelf = self;
        [self.refurbish headerToScollview:self.tableView Block:^{
            weakSelf.page = 1;
            [weakSelf netWorking_header];
            
        }];
        [self.refurbish footerToScollview:self.tableView Block:^{
            weakSelf.page++;
            [weakSelf netWorking_footer];
        }];
        
        [self.tableView.mj_header beginRefreshing];
        [self layoutUI];
    }
    return self;
}

#pragma mark - 数据

//下拉刷新
- (void)netWorking_header{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat: @"%ld",(long)self.page];
    
    NSDictionary *paramer  = @{@"PAGE_NUMBER": page,
                               @"SHOW_NUMBER": @"10"};
    [NetWorkingManager postWithUrlString:[NetWorkingURL getpudongorgactionLoginInfolistConcenterAddress] Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray *array = [object objectForKey:@"ORG_DEPTS"];
            
            if (array.count == 0) {
                [YJProgressHUD showMessage:@"没有数据" inView:[UIApplication sharedApplication].keyWindow];
                [weakSelf.tableView.mj_header endRefreshing];
                return ;
                
            }
            
            
            weakSelf.dataSouce = [[NSArray yy_modelArrayWithClass:[BranchPartyOrgizationModel class] json:array] mutableCopy] ;
            [weakSelf.tableView reloadData];
            
            
        }
        
        [weakSelf.tableView.mj_header endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_header endRefreshing];
    }];
    
    
}

//上拉加载
- (void)netWorking_footer{
    __weak typeof(self) weakSelf = self;
    
    NSString *page = [NSString stringWithFormat: @"%ld",(long)self.page];
    NSDictionary *paramer  = @{@"PAGE_NUMBER": page,
                               @"SHOW_NUMBER": @"10"};
    [NetWorkingManager postWithUrlString:[NetWorkingURL getpudongorgactionLoginInfolistConcenterAddress] Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray *array = [object objectForKey:@"ORG_DEPTS"];
            
            if (array.count == 0) {
                [YJProgressHUD showMessage:@"没有数据" inView:[UIApplication sharedApplication].keyWindow];
                [weakSelf.tableView.mj_footer endRefreshing];
                return ;
                
            }
            
            [weakSelf.dataSouce addObjectsFromArray:[NSArray yy_modelArrayWithClass:[BranchPartyOrgizationModel class] json:array]] ;
            [weakSelf.tableView reloadData];
            
            
        }
        [weakSelf.tableView.mj_footer endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
    
    
}


#pragma mark - 界面
- (void)layoutUI{
    [self addSubview:self.tableView];
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 5)];
    self.tableView.tableHeaderView = header;
}


#pragma mark - 协议代理

/**  UITableViewDataSource,UITableViewDelegate  */
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
    
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSouce.count;
    
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120 * [UIScreen zj_screenHScale];
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *weizhi = [NSString stringWithFormat:@"%ld-%ld",(long)indexPath.section,(long)indexPath.row];
    BranchPostionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
    if (!cell) {
        cell = [[BranchPostionTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
        
    }
    cell.model = self.dataSouce[indexPath.row];
   

    return cell;
}

/**  cell的点击事件  */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"PostioonModel" object:self userInfo:@{@"model": self.dataSouce[indexPath.row]}];
}

#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.tableFooterView = [UIView new];
        _tableView.backgroundColor = [UIColor themeGrayColor];
    }
    return _tableView;
}

- (NSMutableArray *)dataSouce{
    if (!_dataSouce) {
        _dataSouce = [NSMutableArray array];
    }
    return _dataSouce;
}



- (ZJRefurbish *)refurbish{
    if (!_refurbish) {
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}



@end
